<template>
    <div class="list" ref="wrapper">
        <div>
        <div class="area">
            <div class="title border-topbottom">您的位置</div>
            <div class="city-list">
                <div class="btn-wrapper">
                    <!-- <div class="btn">{{this.$store.state.city}}</div> -->
                    <div class="btn" @click="handleCityClick(currentCity)">{{this.currentCity}}</div>
                </div>
            </div>
        </div>
           <div class="area">
            <div class="title border-topbottom">热门城市</div>
            <div class="city-list">
                <div class="btn-wrapper" v-for="item of hotCities" :key="item.id" @click="handleCityClick(item.name)">
                    <div class="btn">{{item.name}}</div>
                </div>
            </div>
        </div>
        <div class="area" v-for="(item,key,index) of cities" :key='index' :ref='key'>
            <div class="title border-topbottom">{{key}}</div>
            <ul class="city-alpha">
                <li class="city-alpha-item border-bottom" v-for='innerItem of item' :key='innerItem.id' @click="handleCityClick(innerItem.name)">{{innerItem.name}}</li>
            </ul>
        </div>
        </div>
    </div>
</template>
<script>
import {mapState, mapMutations} from 'vuex'
import BScroll from 'better-scroll'
// const wrapper = document.querySelector('.wrapper')
export default {
  name: 'CityList',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState({currentCity: 'city'})
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  mounted () {
    const wrapper = this.$refs.wrapper
    this.scroll = new BScroll(wrapper)
  },
  methods: {
    ...mapMutations(['changeCity']),
    handleCityClick (city) {
      //   this.$store.dispatch('handleCityChange', city)
      //   this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    }
  }
}
</script>
<style lang="stylus" scoped>
.unreal
    height:500px
.border-topbottom
    &:before
        border-color:#ccc
    &:after
        border-color:#ccc
.list
    position :absolute
    top:1.58rem
    right:0
    bottom:0
    left:0
    overflow :hidden
    .area
        .title
            height:.44rem
            line-height:.44rem
            color:#333
            background:#eee
            padding-left:.2rem
        .city-list
            padding:.1rem .6rem .1rem .1rem
            overflow :hidden
            .btn-wrapper
                width:33.3%
                float :left
                .btn
                    margin:.1rem
                    text-align:center
                    border-radius:.06rem
                    border:.02rem solid #ccc
                    padding:.1rem 0
        .city-alpha-item
            padding-left:.2rem
            line-height :.44rem
</style>
